# -*- coding:utf-8 -*-
from django.db import models

# Create your models here.

class Resources(models.Model):
    md5 = models.CharField('MD5值', max_length=33,unique=True)
    realPath=models.CharField('真实路径',max_length=255,unique=True)
    fileSize=models.IntegerField('文件大小')
    uploadTime=models.DateTimeField('上传时间',auto_now_add=True)
    filetype=models.CharField('文件类型', max_length=20)
    num=models.IntegerField('引用数量', default=1)
    def __str__(self):
        return '{}'.format(self.realPath )

class User(models.Model):
    username=models.CharField('用户名', max_length=40)
    password=models.CharField('密码', max_length=40)
    admin = models.IntegerField(default=0)   # 0表示不是管理员， 1-15是管理员,-1封号
    vip = models.ForeignKey('VIP',to_field='grade', on_delete=models.CASCADE)
    def __str__(self):
        return '{}-{}-{}'.format(self.username,self.password,self.vip)

# 当作sesion用
class Token(models.Model):
    userID=models.CharField(max_length=32)
    user = models.ForeignKey(User, on_delete=models.CASCADE)  # 所属用户
    lastTime=models.DateTimeField('上次登陆时间')
    def __str__(self):
        return '{}-{}'.format(self.userID,self.lastTime)

class ResList(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    res = models.ForeignKey(Resources, on_delete=models.CASCADE)
    filename=models.CharField('文件名', max_length=50, default='未命名')
    # lastTime = models.TimeField('上次播放位置',default='00:00:00')
    def __str__(self):
        return '{}-{}-{}-{}'.format(self.filename, self.user,self.res,self.lastTime)


class VIP(models.Model):
    grade=models.IntegerField(unique=True)
    size = models.IntegerField('存储空间')
    def __str__(self):
        return '{}-{}'.format(self.grade,self.size)


'''
python3 manage.py makemigrations
python3 manage.py migrate

# 初始化命令
python3 manage.py shell
from app1.models import *
from app1 import utils
VIP.objects.create(grade=1, size=1)
VIP.objects.create(grade=2, size=2)
VIP.objects.create(grade=3, size=3)
# 默认创建一个最高级别(15)的管理员，用户名和密码都是admin
User.objects.create(username="admin", password=utils._getMD5("admin"), admin=15, vip_id=3)
'''

